import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import axios from "../utils/request5.js"
export default new Vuex.Store({
  state: {
    article: {},
    list: [],
    priceIndexList: 0,
    frientList:[]
  },

  getters: {
    //计算商品的总价格
    allPrice(state) {
      return state.list.reduce((prve, next) => {
        if (next) {
          return prve + next.price * next.num;
        } else {
          return prve;
        }
      }, 0);
    },
  },
  mutations: {
    //让每个单选和多选状体变得一样
    CHANG_ITEM_FLAG(state, actions) {
      console.log("actions...", actions);
      state.list.forEach((item) => (item.flag = actions));
    },
    //详情数据
    SET_INDEX_LIST(state, payload) {
      state.article = payload;
    },
    //
    SET_LIST_MAP(state, payload) {
      console.log("state", state);
      let flag = state.list.findIndex((item) => {
        return item.id === payload.id;
      });
      if (flag === -1) {
        state.list.push(payload);
      } else {
        payload = state.list[flag];
        payload.num++;
        state.priceIndexList = payload.num;
      }
    },
    //购物车图标数据加加
    SET_DATA_LIST_INDEX(state, payload) {
      state.priceIndexList = payload;
    },
    //好友信息
    GETFRIEND_LIST(state, payload){
        state.frientList=payload.friendList
        console.log("11",state.frientList)
    }
  },
  actions: {
    //好友信息
    FriendMsg({commit}){
     axios.post("/api/firend").then(res=>{
       commit("GETFRIEND_LIST",{friendList:res.friedList})
       console.log(res)
     })
    }
  },
  modules: {},
});
